package org.redis.cluster.redis.processor;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.redis.cluster.RedisCluster;
import org.redis.cluster.extension.ExtensionLoader;
import org.redis.cluster.plugin.Plugin;
import org.redis.cluster.processor.RedisReplyProcessor;
import org.redis.cluster.redis.transport.RedisReply;
import org.redis.cluster.redis.transport.reply.RedisMultipleBulkReply;

public class RedisMultipleBulkReplyProcessor implements RedisReplyProcessor<RedisMultipleBulkReply>,Plugin {
	
	private final static AtomicBoolean IS_INIT=new AtomicBoolean(false);
	
	private final static int OTHER_BYTE_COUNT=3;
	
	private final static byte[] EMPTY="$-1\r\n".getBytes();
	
	private RedisCluster redisCluster;
	private ExtensionLoader extensionLoader;
	private Map<Class<? extends RedisReply>,RedisReplyProcessor> processorMapping;
	
	@Override
	public void init(RedisCluster redisCluster) {
		this.redisCluster=redisCluster;
		this.extensionLoader=redisCluster.getExtensionLoader();
		
		if(!IS_INIT.get()) {
			IS_INIT.set(true);
			this.processorMapping=initProcessorMapping();
		}
	}
		
	protected Map<Class<? extends RedisReply>,RedisReplyProcessor> initProcessorMapping() {
		Collection<RedisReplyProcessor> processors=extensionLoader.load(RedisReplyProcessor.class);
		if(CollectionUtils.isEmpty(processors)) {
			return MapUtils.EMPTY_MAP;
		}
		
		Map<Class<? extends RedisReply>,RedisReplyProcessor> result=new HashMap<Class<? extends RedisReply>,RedisReplyProcessor>();
		for(RedisReplyProcessor processor:processors) {
			result.put(processor.getSupportClass(), processor);
		}
		
		return result;
	}

	@Override
	public void destroy() {
	}
	
	@Override
	public byte[] process(RedisMultipleBulkReply reply) {
		Collection<RedisReply> values=reply.getValues();
		if(CollectionUtils.isEmpty(values)) {
			return EMPTY;
		}
		
		byte bytes[][]=new byte[values.size()][];
		Iterator<RedisReply> iterator=values.iterator();
		byte parameterCountByte[]=String.valueOf(values.size()).getBytes();
		int length=OTHER_BYTE_COUNT+parameterCountByte.length;
		
		for(int i=0;i<bytes.length;i++) {
			RedisReply redisReply=iterator.next();
			if(redisReply==null) {
				length+=EMPTY.length;
				bytes[i]=EMPTY;
			}
			else {
				byte itemByte[]=doReplyProcess(redisReply);
				if(itemByte==null) {
					length+=EMPTY.length;
					bytes[i]=EMPTY;
				}
				else {
					length+=itemByte.length;
					bytes[i]=itemByte;
				}
			}
		}
		
		ByteBuffer buffer=ByteBuffer.allocate(length);
		buffer.put(RedisReply.COUNT_START);
		buffer.put(parameterCountByte);
		buffer.put(RedisReply.END_1);
		buffer.put(RedisReply.END_2);
		
		for(int i=0;i<bytes.length;i++) {
			buffer.put(bytes[i]);
		}
		
		return buffer.array();
	}

	@Override
	public Class<? extends RedisReply> getSupportClass() {
		return RedisMultipleBulkReply.class;
	}
	
	protected byte[] doReplyProcess(RedisReply redisReply) {
		RedisReplyProcessor processor=processorMapping.get(redisReply.getClass());
		if(processor==null) {
			return null;
		}
		
		return processor.process(redisReply);
	}
	
	public void setReplyProcessorMapping(Map<Class<? extends RedisReply>, RedisReplyProcessor> replyProcessorMapping) {
		this.processorMapping = replyProcessorMapping;
	}
	public Map<Class<? extends RedisReply>, RedisReplyProcessor> getReplyProcessorMapping() {
		return processorMapping;
	}
}